local DEFAULT = "#showtooltip Hearthstone\n/use Hearthstone"

local addon = {}
addon.frame = CreateFrame("Frame", "gMacro", UIParent)

addon.events = {
	"ZONE_CHANGED",
	"PLAYER_TARGET_CHANGED",
	"PLAYER_ENTERING_WORLD",
}

addon.db = {
	["Erratic Sentry"] 			= 	"#showtooltip item:34368\n/use item:34368",
	["Sun's Reach Sanctum"] 	=	"#showtooltip item:34338\n/use item:34338",
	["Wretched Fiend"] 			=	"#showtooltip item:34338\n/use item:34338",
	["Wretched Devourer"] 		=	"#showtooltip item:34338\n/use item:34338",
	["The Dead Scar"] 			=	"#showtooltip item:34475\n/use item:34475",
	["Emissary of Hate"] 		=	"#showtooltip item:34414\n/use item:34414",
	["The Dawning Square"] 		=	"#showtooltip item:34533\n/use item:34533",
	["Dawnstar Village"] 		=	"#showtooltip item:34533\n/use item:34533",
	["The North Sea"] 			=	"#showtooltip item:34489\n/use item:34489",
	["The Sin'loren"] 			=	"#showtooltip item:34489\n/use item:34489",
	["The Dawnchaser"]			=	"#showtooltip item:34489\n/use item:34489",
	["The Bloodoath"]			=	"#showtooltip item:34489\n/use item:34489",
	["Terestian Illhoof"] 		=	"#show spell:9484\n/target Demon Chains\n/startattack\n/stopmacro [nodead]\n/target Illhoof",
	["Greengill Coast"] 		= 	"#showtooltip item:34533\n/use item:34533",
	["Greengill Slave"]			=	"#showtooltip item:34483\n/use item:34483",
	["Razorthorn Rise"] 		= 	"#showtooltip [pet] Expose Razorthorn Root; item:34255\n/use [nopet] item:34255\n/cast [pet] Expose Razorthorn Root",
	["The Curator"] 			= 	"#show item:34483\n/target Astral Flare\n/startattack",
	["The Menagerie"]			=	"#show item:34483\n/target Astral Flare\n/startattack",
	["Spirit Fields"] 			= 	"#showtooltip item:35233\n/equip item:35233\n/use item:35233",
	["Bash'ir Landing"] 		= 	"#showtooltip item:34248\n/use item:34248",
	["The Black Morass"] 		= 	"#showtooltip item:24289\n/use item:24289",
	["Felblood Initiate"]		=	"#showtooltip item:34257\n/use item:34257",
	["Throne of Kil'jaeden"] 	=	"#showtooltip item:34253\n/use item:34253",
	["Skettis"]					=	"#showtooltip item:32406\n/use item:32406",
	["Veil Ala'rak"]			=	"#showtooltip item:32406\n/use item:32406",
	["Upper Veil Shil'ak"]		=	"#showtooltip item:32406\n/use item:32406",
	["Lower Veil Shil'ak"]		=	"#showtooltip item:32406\n/use item:32406",
}

function addon.frame:PLAYER_ENTERING_WORLD(...)
	if ( not GetMacroInfo("gMacro") ) then
		CreateMacro("gMacro", 1, DEFAULT, 1)
	end
	
	addon.macro = GetMacroIndexByName("gMacro")
	
	self:ZONE_CHANGED()
end

function addon.frame:PLAYER_TARGET_CHANGED(...)
	local name = UnitName("target")
	if ( not(name and addon.db[name]) or InCombatLockdown() ) then
		self:ZONE_CHANGED()
		return
	end

	EditMacro(addon.macro, "gMacro", 1, addon.db[name], 1)	
end

function addon.frame:ZONE_CHANGED(...)
	local zone = GetMinimapZoneText()
	if ( not(zone and addon.db[zone]) or InCombatLockdown() ) then
		return
	end

	EditMacro(addon.macro, "gMacro", 1, addon.db[zone], 1)
end

addon.frame:SetScript("OnEvent", function(self, event, ...)
	self[event](self, ...)
end)

for i, v in ipairs(addon.events) do
	addon.frame:RegisterEvent(v)
end
